﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Workflow.ComponentModel.Compiler;
using System.Reflection;

namespace WorkflowDesignerApp
{
    public partial class AssemblyReferenceForm : Form
    {
        private TypeProvider _typeProvider;

        public AssemblyReferenceForm(TypeProvider provider)
        {
            InitializeComponent();
            //build the list of referenced assemblies
            _typeProvider = provider;
            if (_typeProvider != null)
            {
                PopulateListWithReferences();
            }
        }

        /// <summary>
        /// Build the list of referenced assemblies
        /// </summary>
        private void PopulateListWithReferences()
        {
            listReferences.Items.Clear();
            foreach (Assembly assembly in
                _typeProvider.ReferencedAssemblies)
            {
                listReferences.Items.Add(assembly);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.InitialDirectory = Environment.CurrentDirectory;
            openFile.Filter
                = "Dll files (*.Dll)|*.Dll|All files (*.*)|*.*";
            openFile.FilterIndex = 1;
            openFile.Multiselect = true;
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                foreach (String filename in openFile.FileNames)
                {
                    //add the referenced assemblies to the TypeProvider
                    _typeProvider.AddAssemblyReference(filename);
                }
                PopulateListWithReferences();
            }
        }
    }
}
